package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    //从注册中心引用 CheckGroupService
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/add")//二级路径
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")//权限校验
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //添加成功
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 根据id查询
     * @param checkgroupId
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer checkgroupId) {
        //根据id查询检查组
        CheckGroup checkGroup = checkGroupService.findById(checkgroupId);
        //判断检查组是否为空
        if (checkGroup != null) {
            //检查组不为空
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroup);
            return result;
        }
        //检查组为空
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 根据检查组id查询对应的检查项id
     * @param checkgroupId
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        try {
            //根据检查组id查询对应的检查项ids
            List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(checkgroupId);
            //查询成功 将检查项ids作为参数返回
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败,返回失败信息
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //根据ID删除
            checkGroupService.delete(id);
            //删除成功
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 修改
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            //进行编辑操作
            checkGroupService.edit(checkGroup, checkitemIds);
            //编辑成功   返回成功信息
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑失败  返回失败信息
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            //执行查询所有操作
            List<CheckGroup> list = checkGroupService.findAll();
            //执行成功,将结果作为参数返回
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败 返回失败信息
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
